<template>
  <div class="app-container">
    <el-row :gutter="20" class="question-edit-contain">
      <el-col :span="24">
        <el-card>
          <el-form
            :model="form"
            ref="form"
            label-width="100px"
            v-loading="formLoading"
            :rules="rules"
          >
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="问卷名称：" prop="surveyName" required>
                  <el-input v-model="form.surveyName"> </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-card class="box-card" style="height: 350px; overflow-y: auto">
              <div slot="header" class="clearfix">
                <span style="font-size: 16px; font-weight: bold">评价问卷内容</span>
                <el-button style="float: right; padding: 3px 0" type="text" @click="addSubject()"
                  >添加评价</el-button
                >
              </div>
              <div v-for="(item, index) in form.subjects" :key="item.detailId" class="text item">
                <el-row :gutter="20">
                  <el-col :span="20">
                    <div>{{ index + 1 }}、{{ item.subjectTitle }}</div>
                  </el-col>
                  <el-col :span="4">
                    <el-button type="danger" size="small" @click="removeSubject(item)"
                      >删除</el-button
                    >
                  </el-col>
                </el-row>
              </div>
            </el-card>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item>
                  <el-button type="primary" @click="submitForm">提交</el-button>
                  <el-button @click="resetForm">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import formsChooseBizEvluateSurvey from '@/views/biz/formsChooseBizEvluateSurvey/index.vue';
import { BizEvluateSurveyController } from '@/api';
export default {
  name: 'formsEditBizEvluateSurvey',
  props: {
    rowData: Object
  },
  data() {
    return {
      form: {
        id: null,
        moduleType: 5,
        surveyName: null,
        subjects: []
      },
      formLoading: false,
      rules: {
        surveyName: [{ required: true, message: '请输入问卷名称', trigger: 'blur' }]
      }
    };
  },
  created() {},
  methods: {
    onCancel(isSuccess = false) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess);
      }
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.subjects.length === 0) {
            this.$message.warning('请录入评价!');
            return false;
          }
          this.formLoading = true;
          BizEvluateSurveyController.edit(this, this.form)
            .then(re => {
              console.log(re);
              if (re.errorCode === 'NO-ERROR') {
                this.$message.success('操作成功');
                this.onCancel(true);
                this.$router.replace({ name: 'formsBizEvluateSurvey' });
              } else {
                this.$message.error('操作失败');
                console.error(re.errorMessage);
              }
            })
            .finally(() => {
              this.formLoading = false;
            });
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.form = {
        id: null,
        surveyName: null,
        subjects: []
      };
      this.$refs['form'].resetFields();
    },
    loadExistSubjectData() {
      return new Promise((resolve, reject) => {
        let params = {
          id: this.form.id
        };
        BizEvluateSurveyController.detail(this, params).then(res => {
          this.form.subjects = res.data;
        });
      });
    },
    addSubject() {
      let data = { ...this.rowData, includedSubjects: this.form.subjects };
      if (!data.personnelType) {
        data = { ...data, personnelType: this.form.personnelType };
      }
      this.$dialog
        .show(
          '选择评价',
          formsChooseBizEvluateSurvey,
          {
            area: '960px'
          },
          {
            rowData: data
          }
        )
        .then(res => {
          this.form.subjects = res;
        })
        .catch(e => {});
    },
    removeSubject(subject) {
      this.form.subjects = this.form.subjects.filter(item => item.subjectId !== subject.subjectId);
    }
  },
  mounted() {
    if (Object.keys(this.rowData).length > 0) {
      this.form.id = this.rowData.id;
      this.form.surveyName = this.rowData.surveyName;
    }
    if (Object.keys(this.rowData).length > 0 && this.rowData.id) {
      this.loadExistSubjectData();
    }
  }
};
</script>
<style type="scss" scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
  margin-bottom: 20px;
  padding: 20px;
}
</style>
